﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Doo.WebUI;
using System.Collections.Specialized;

namespace Doo.ServerControls
{
    public class LinkProxyValueGettedEventArgs : EventArgs
    {
        public Control SourceControl { get; set; }
        public IOrderedDictionary Values { get; set; }
    }

    public class LinkProxy : Control
    {
        [IDReferenceProperty]
        public string ParentControlID { get; set; }

        [IDReferenceProperty]
        public string ChildControlID { get; set; }

        protected Control parentcontrol;
        protected ILinkControl parent;
        protected Control childcontrol;
        protected ILinkControl child;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            parentcontrol = this.NamingContainer.FindControl(ParentControlID);
            childcontrol = this.NamingContainer.FindControl(ChildControlID);
            if (parentcontrol is ILinkControl)
            {
                parent = (ILinkControl)parentcontrol;
                parent.ValueChanged += new EventHandler(parent_ValueChanged);
            }

            if (childcontrol is ILinkControl)
            {
                child = (ILinkControl)childcontrol;
                child.ValueChanged += new EventHandler(child_ValueChanged);
            }
            if (ParentEventName != null)
            {
                var evt = parentcontrol.GetType().GetEvent(ParentEventName);
                ParentValueChanged += new EventHandler(LinkProxy_ParentValueChanged);
                evt.AddEventHandler(parentcontrol, ParentValueChanged);
            }
        }

        void LinkProxy_ParentValueChanged(object sender, EventArgs e)
        {
            //获取值
            IOrderedDictionary values = GetValues(parentcontrol);
            child.ParentValueChanged(parentcontrol, values);
        }

        void child_ValueChanged(object sender, EventArgs e)
        {
            if (parent!=null) parent.ChildValueChanged(childcontrol, child.Values);
            if (ChildValueChanged!=null) this.ChildValueChanged(sender, e);
        }

        void parent_ValueChanged(object sender, EventArgs e)
        {
            if (child!=null) child.ParentValueChanged(parentcontrol, parent.Values);
            if (ParentValueChanged != null) this.ParentValueChanged(sender, e);
        }

        public event EventHandler ParentValueChanged;

        public event EventHandler ChildValueChanged;

        public string ParentPropertyName { get; set; }

        public string ParentEventName { get; set; }

        public event EventHandler<LinkProxyValueGettedEventArgs> ParentValuesGetting;

        protected System.Collections.Specialized.IOrderedDictionary GetValues(Control ctl)
        {
            var values = new OrderedDictionary();

            if (ParentValuesGetting != null)
            {
                LinkProxyValueGettedEventArgs args = new LinkProxyValueGettedEventArgs();
                args.Values = values;
                args.SourceControl = ctl;
                this.ParentValuesGetting(this, args);
                return args.Values;
            }

            var pinfo = ctl.GetType().GetProperty(ParentPropertyName);
            object value = pinfo.GetValue(ctl, null);
            values.Add("value", value);
            return values;

        }

    }
}
